﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using XiaoGang.Soft.XGWMS.BLL;
using XiaoGang.Soft.XGWMS.MODEL;
using XiaoGang.Soft.XGWMS.COM;
using System.IO;
using System.Drawing.Imaging;

namespace XiaoGang.Soft.XGWMS.UI
{
    public partial class M012MemberEdit : FormBase
    {
        #region 变量定义

        enumOperation MOpertion = new enumOperation();
        bool boolClear = false;
        int intFirstDisplayedScrollingRowIndex = 0;
        private readonly MOD_M012_MEMBER modMember;
        private readonly BLL_M012_MEMBER bllMember;
        /// <summary>
        /// 会员
        /// </summary>
        private const string dataName = "会员";

        #endregion

        #region 方法

        /// <summary>
        /// 构造函数
        /// </summary>
        public M012MemberEdit()
        {
            InitializeComponent();
            modMember = new MOD_M012_MEMBER();
            bllMember = new BLL_M012_MEMBER();
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="Member"></param>
        public M012MemberEdit(MOD_M012_MEMBER Member)
        {
            InitializeComponent();
            modMember = Member;
            bllMember = new BLL_M012_MEMBER();
        }

        /// <summary>
        /// Initial
        /// </summary>
        private void Initial()
        {
            SetControlsIndex();
            InitialControl();
            txtUSER_ID.Enabled = false;
        }

        /// <summary>
        /// SetControlsIndex
        /// </summary>
        protected void SetControlsIndex()
        {
            int intIndex = 0; intIndex++;
            txtUSER_NO.TabIndex = intIndex; intIndex++;
            txtUSER_NAME.TabIndex = intIndex; intIndex++;
            txtUSER_BIRTHDAY.TabIndex = intIndex; intIndex++;
            grpSex.TabIndex = intIndex; intIndex++;
            txtUSER_PHONE1.TabIndex = intIndex; intIndex++;
            txtUSER_PHONE2.TabIndex = intIndex; intIndex++;
            txtUSER_ADDRESS1.TabIndex = intIndex; intIndex++;
            txtUSER_ADDRESS2.TabIndex = intIndex; intIndex++;
            txtUSER_EMAIL.TabIndex = intIndex; intIndex++;
            txtUSER_POST.TabIndex = intIndex; intIndex++;
            txtMEMO.TabIndex = intIndex; intIndex++;
            btnPicSearch.TabIndex = intIndex; intIndex++;
            btnPicClear.TabIndex = intIndex; intIndex++;
            btnAdd.TabIndex = intIndex; intIndex++;
            btnClear.TabIndex = intIndex; intIndex++;
            btnExit.TabIndex = intIndex; intIndex++;
            txtUSER_ID.TabIndex = intIndex; intIndex++;
        }

        /// <summary>
        /// InitialControl
        /// </summary>
        private void InitialControl()
        {
            lbImageMaxSize.Text = lbImageMaxSize.Text.Replace("@", GlobalVal.gloImageMaxSize.ToString());
            this.txtUSER_ID.Text = modMember.USER_ID;
            this.txtUSER_NO.Text = modMember.USER_NO;
            this.txtUSER_NAME.Text = modMember.USER_NAME;
            this.dateTimePickerUSER_BIRTHDAY.Text = modMember.USER_BIRTHDAY;
            this.txtUSER_BIRTHDAY.Text = modMember.USER_BIRTHDAY;
            this.txtUSER_PHONE1.Text = modMember.USER_PHONE1;
            this.txtUSER_PHONE2.Text = modMember.USER_PHONE2;
            if (string.IsNullOrEmpty(modMember.USER_SEX) || modMember.USER_SEX == "0")
            {
                rdoBSEX_M.Checked = true;
            }
            else
            {
                rdoBSEX_F.Checked = true;
            }
            this.txtUSER_ADDRESS1.Text = modMember.USER_ADDRESS1;
            this.txtUSER_ADDRESS2.Text = modMember.USER_ADDRESS2;
            this.txtUSER_EMAIL.Text = modMember.USER_EMAIL;
            this.txtUSER_POST.Text = modMember.USER_POST;
            this.txtMEMO.Text = modMember.MEMO;
            ShowImage(modMember.USER_ID);  
        }

        /// <summary>
        /// ShowImage
        /// </summary>
        /// <param name="UserID"></param>
        private void ShowImage(string UserID)
        {
            if (string.IsNullOrEmpty(UserID))
            {
                this.picUSER_PHOTO.Image = null;
                return;
            }
            try
            {
                ReturnValue returnValue = bllMember.GetUserPhoto(UserID);
                if (!returnValue.IsSuccess)
                {
                    return;
                }
                if (returnValue.ResultDataSet.Tables[0].Rows.Count < 1)
                {
                    return;
                }
                MemoryStream ms = new MemoryStream((byte[])returnValue.ResultDataSet.Tables[0].Rows[0]["USER_PHOTO"]);
                Image image = Image.FromStream(ms, true);
                this.picUSER_PHOTO.Image = image;
            }
            catch
            {
                this.picUSER_PHOTO.Image = null;
            }
        }

        /// <summary>
        /// CheckProcessLoop
        /// </summary>
        /// <param name="enumOperation"></param>
        /// <returns></returns>
        protected bool CheckProcessLoop(enumOperation enumOperation)
        {
            switch (enumOperation)
            {                
                case enumOperation.Add:
                    MOpertion = enumOperation.Add;
                    //卡号
                    if (!CheckProcess(txtUSER_NO.Name)) return false;
                    //姓名
                    if (!CheckProcess(this.txtUSER_NAME.Name)) return false;
                    //出生年月日
                    if (!CheckProcess(this.txtUSER_BIRTHDAY.Name)) return false;
                    //电话1
                    if (!CheckProcess(this.txtUSER_PHONE1.Name)) return false;
                    //电话2
                    if (!CheckProcess(this.txtUSER_PHONE2.Name)) return false;
                    //住址1
                    if (!CheckProcess(this.txtUSER_ADDRESS1.Name)) return false;
                    //住址2
                    if (!CheckProcess(this.txtUSER_ADDRESS2.Name)) return false;
                    //E_mail
                    if (!CheckProcess(this.txtUSER_EMAIL.Name)) return false;
                    //邮编
                    if (!CheckProcess(this.txtUSER_POST.Name)) return false;
                    //备注
                    if (!CheckProcess(this.txtMEMO.Name)) return false;
                    break;
                case enumOperation.Update:
                    MOpertion = enumOperation.Update;
                    //卡号
                    if (!CheckProcess(txtUSER_NO.Name)) return false;
                    //编号
                    if (!CheckProcess(this.txtUSER_ID.Name)) return false;
                    //姓名
                    if (!CheckProcess(this.txtUSER_NAME.Name)) return false;
                    //出生年月日
                    if (!CheckProcess(this.txtUSER_BIRTHDAY.Name)) return false;
                    //电话1
                    if (!CheckProcess(this.txtUSER_PHONE1.Name)) return false;
                    //电话2
                    if (!CheckProcess(this.txtUSER_PHONE2.Name)) return false;
                    //住址1
                    if (!CheckProcess(this.txtUSER_ADDRESS1.Name)) return false;
                    //住址2
                    if (!CheckProcess(this.txtUSER_ADDRESS2.Name)) return false;
                    //E_mail
                    if (!CheckProcess(this.txtUSER_EMAIL.Name)) return false;
                    //邮编
                    if (!CheckProcess(this.txtUSER_POST.Name)) return false;
                    //备注
                    if (!CheckProcess(this.txtMEMO.Name)) return false;
                    break;
            }
            return true;
        }

        private bool CheckIsUseUserNo(string UserNO)
        {
            if (UserNO.Length < 1)
            {
                return true;
            }
            BLL_M012_MEMBER bll = new BLL_M012_MEMBER();
            ReturnValue reValue = bll.CheckIsUseUserNo(UserNO);
            if (reValue.IsSuccess)
            {
                if (reValue.ResultDataSet.Tables[0].Rows.Count > 0)
                {
                    Method.MessageShow("W11055", dataName);//本会员有消费记录，不能删除!
                    return false;
                }
                else
                {
                    return true;
                }
            }
            else
            {
                Method.MessageShow(reValue.ErrorID, dataName);
                return false;
            }
        }

        /// <summary>
        /// CheckProcess
        /// </summary>
        /// <param name="strConstrolName"></param>
        /// <returns></returns>
        protected override bool CheckProcess(string strConstrolName)
        {
            string strValue = string.Empty;
            switch (strConstrolName)
            {
                #region 编号
                case "txtUSER_ID":
                    strValue = txtUSER_ID.Text.Trim();
                    if (MOpertion == enumOperation.Search)
                    {
                        if (!CheckProcess(strValue, enumCheckItems.ItemID, true, null))
                        {
                            txtUSER_ID.Focus();
                            txtUSER_ID.SelectAll();
                            return false;
                        }
                    }
                    else if (MOpertion == enumOperation.Add)
                    {
                        break;
                    }
                    else
                    {
                        if (!CheckProcess(strValue, enumCheckItems.ItemID, false, null))
                        {
                            txtUSER_ID.Focus();
                            txtUSER_ID.SelectAll();
                            return false;
                        }
                    }
                    break;
                #endregion

                #region 会员号
                case "txtUSER_NO":
                    strValue = txtUSER_NO.Text.Trim();
                    if (MOpertion == enumOperation.Search)
                    {
                        if (!CheckProcess(strValue, enumCheckItems.MemberNo, true, null))
                        {
                            txtUSER_NO.Focus();
                            txtUSER_NO.SelectAll();
                            return false;
                        }
                    }
                    else
                    {
                        if (!CheckProcess(strValue, enumCheckItems.MemberNo, false, null))
                        {
                            txtUSER_NO.Focus();
                            txtUSER_NO.SelectAll();
                            return false;
                        }
                    }
                    break;
                #endregion

                #region 姓名
                case "txtUSER_NAME":
                    strValue = txtUSER_NAME.Text.Trim();
                    if (MOpertion == enumOperation.Search)
                    {
                        if (!CheckProcess(strValue, enumCheckItems.UserName, true, null))
                        {
                            txtUSER_NAME.Focus();
                            txtUSER_NAME.SelectAll();
                            return false;
                        }
                    }
                    else
                    {
                        if (!CheckProcess(strValue, enumCheckItems.UserName, false, null))
                        {
                            txtUSER_NAME.Focus();
                            txtUSER_NAME.SelectAll();
                            return false;
                        }
                    }
                    break;
                #endregion

                #region 出生年月日
                case "txtUSER_BIRTHDAY":
                    strValue = txtUSER_BIRTHDAY.Text.Trim();
                    if (!CheckProcess(strValue, enumCheckItems.DateYMD, true, null))
                    {
                        txtUSER_BIRTHDAY.Focus();
                        txtUSER_BIRTHDAY.SelectAll();
                        return false;
                    }
                    break;

                #endregion

                #region 电话1
                case "txtUSER_PHONE1":
                    strValue = txtUSER_PHONE1.Text.Trim();
                    if (MOpertion == enumOperation.Search)
                    {
                        if (!CheckProcess(strValue, enumCheckItems.PHONE_NO, true, null))
                        {
                            txtUSER_PHONE1.Focus();
                            txtUSER_PHONE1.SelectAll();
                            return false;
                        }
                    }
                    else
                    {
                        if (!CheckProcess(strValue, enumCheckItems.PHONE_NO, false, null))
                        {
                            txtUSER_PHONE1.Focus();
                            txtUSER_PHONE1.SelectAll();
                            return false;
                        }
                    }
                    break;
                #endregion

                #region 电话2
                case "txtUSER_PHONE2":
                    strValue = txtUSER_PHONE2.Text.Trim();
                    if (!CheckProcess(strValue, enumCheckItems.PHONE_NO, true, null))
                    {
                        txtUSER_PHONE2.Focus();
                        txtUSER_PHONE2.SelectAll();
                        return false;
                    }
                    break;
                #endregion

                #region 住址1
                case "txtUSER_ADDRESS1":
                    strValue = txtUSER_ADDRESS1.Text.Trim();
                    if (!CheckProcess(strValue, enumCheckItems.ItemMEMO, true, null))
                    {
                        this.txtUSER_ADDRESS1.Focus();
                        txtUSER_ADDRESS1.SelectAll();
                        return false;
                    }
                    break;
                #endregion

                #region 住址2
                case "txtUSER_ADDRESS2":
                    strValue = txtUSER_ADDRESS2.Text.Trim();
                    if (!CheckProcess(strValue, enumCheckItems.ItemMEMO, true, null))
                    {
                        this.txtUSER_ADDRESS2.Focus();
                        txtUSER_ADDRESS2.SelectAll();
                        return false;
                    }
                    break;
                #endregion

                #region E_mail
                case "txtUSER_EMAIL":
                    strValue = txtUSER_EMAIL.Text.Trim();
                    if (!CheckProcess(strValue, enumCheckItems.E_Mail, true, null))
                    {
                        this.txtUSER_EMAIL.Focus();
                        txtUSER_EMAIL.SelectAll();
                        return false;
                    }
                    break;
                #endregion

                #region 邮编
                case "txtUSER_POST":
                    strValue = txtUSER_POST.Text.Trim();
                    if (!CheckProcess(strValue, enumCheckItems.Post, true, null))
                    {
                        this.txtUSER_POST.Focus();
                        this.txtUSER_POST.SelectAll();
                        return false;
                    }
                    break;
                #endregion

                #region 备注
                case "txtMEMO":
                    strValue = txtMEMO.Text.Trim();
                    if (!CheckProcess(strValue, enumCheckItems.ItemMEMO, true, null))
                    {
                        this.txtMEMO.Focus();
                        txtMEMO.SelectAll();
                        return false;
                    }
                    break;
                #endregion
            }
            return true;
        }

        //private bool CheckRepeatUserNo(string UserNO)
        //{
        //    if (UserNO.Trim().Length < 1)
        //    {
        //        return true;
        //    }
        //    BLL_M012_MEMBER bll = new BLL_M012_MEMBER();
        //    ReturnValue reValue = bll.CheckUserNoExist(UserNO);
        //    if (reValue.IsSuccess)
        //    {
        //        if (reValue.ResultDataSet.Tables[0].Rows.Count > 0)
        //        {
        //            Method.MessageShow("W11E01", dataName);//本[@]已经存在。
        //            return false;
        //        }
        //        else
        //        {
        //            return true;
        //        }
        //    }
        //    else
        //    {
        //        Method.MessageShow(reValue.ErrorID, dataName);
        //        return false;
        //    }
        //}

        private bool CheckRepeatUserNo(string UserNO, string UserID)
        {
            if (UserNO.Trim().Length < 1)
            {
                return true;
            }
            BLL_M012_MEMBER bll = new BLL_M012_MEMBER();
            ReturnValue reValue = bll.CheckUserNoExist(UserNO, UserID);
            if (reValue.IsSuccess)
            {
                if (reValue.ResultDataSet.Tables[0].Rows.Count > 0)
                {
                    Method.MessageShow("W11E01", dataName);//本[@]已经存在。
                    return false;
                }
                else
                {
                    return true;
                }
            }
            else
            {
                Method.MessageShow(reValue.ErrorID, dataName);
                return false;
            }
        }

        #endregion        

        #region 事件

        /// <summary>
        /// M012_MEMBER_Load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void M012_MEMBER_Load(object sender, EventArgs e)
        {
            Initial();
        }

        /// <summary>
        /// btnClear_Click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClear_Click(object sender, EventArgs e)
        {
            if (this.Cursor == Cursors.WaitCursor)
            {
                return;
            }
            InitialControl();
            txtUSER_NAME.Focus();
        }

        /// <summary>
        /// btnExit_Click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnExit_Click(object sender, EventArgs e)
        {
            if (this.Cursor == Cursors.WaitCursor)
            {
                return;
            }
            this.Close();
        }        

        /// <summary>
        /// M012_MEMBER_KeyUp
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void M012_MEMBER_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.F5:
                    btnAdd_Click(sender, e);
                    break;
                case Keys.F12:
                    btnClear_Click(sender, e);
                    break;
                case Keys.Escape:
                    btnExit_Click(sender, e);
                    break;
            }
        }

        /// <summary>
        /// btnAdd_Click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (this.Cursor == Cursors.WaitCursor)
            {
                return;
            }
            if (modMember.USER_ID.Length < 1 && !this.CheckProcessLoop(enumOperation.Add))
            {
                return;
            }
            if (modMember.USER_ID.Length > 0 && !this.CheckProcessLoop(enumOperation.Update))
            {
                return;
            }
            this.Cursor = Cursors.WaitCursor;
            
            ReturnValue returnValue;
            modMember.UPDATER_ID = GlobalVal.gloUserInfo.USER_ID;
            modMember.TERMINAL_CD = GlobalVal.gloUserInfo.TERMINAL_CD;
            modMember.USER_NO = this.txtUSER_NO.Text.Trim();
            modMember.USER_NAME = this.txtUSER_NAME.Text.Trim();
            if (rdoBSEX_M.Checked)
            {
                modMember.USER_SEX = "0";
            }
            else
            {
                modMember.USER_SEX = "1";
            }
            modMember.USER_BIRTHDAY = this.txtUSER_BIRTHDAY.Text.Trim().Replace("/", "");
            modMember.USER_PHONE1= this.txtUSER_PHONE1.Text.Trim();
            modMember.USER_PHONE2 = this.txtUSER_PHONE2.Text.Trim();
            modMember.USER_ADDRESS1 = this.txtUSER_ADDRESS1.Text.Trim();
            modMember.USER_ADDRESS2 = this.txtUSER_ADDRESS2.Text.Trim();
            modMember.USER_EMAIL = this.txtUSER_EMAIL.Text.Trim();
            modMember.USER_POST = this.txtUSER_POST.Text.Trim();
            //************************************************
            if (picUSER_PHOTO.Image != null)
            {
                MemoryStream ms = new MemoryStream();
                picUSER_PHOTO.Image.Save(ms, ImageFormat.Jpeg);
                Byte[] buffer = new Byte[ms.Length];
                ms.Position = 0;
                ms.Read(buffer, 0, Convert.ToInt32(ms.Length));
                modMember.USER_PHOTO = buffer;
            }
            //************************************************
            else
            {
                modMember.USER_PHOTO = null;
            }
            modMember.MEMO =this.txtMEMO.Text.Trim();
            try
            {
                returnValue = bllMember.AddOrUpdateMember(modMember);
                if (!returnValue.IsSuccess)
                {
                    Method.MessageShow(returnValue.ErrorID, dataName);
                    this.Cursor = Cursors.Default;
                    return;
                }
                else
                {
                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                    this.Close();
                }
            }
            catch
            {
                Method.MessageShow("E11004",dataName);//An unknown error process!
            }
            this.Cursor = Cursors.Default;
        }

        /// <summary>
        /// btnPicSearch_Click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPicSearch_Click(object sender, EventArgs e)
        {
            if (this.Cursor == Cursors.WaitCursor)
            {
                return;
            }
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "所有图片(*.bmp;*.gif;*.jpeg;*.jpg;*.png)|*.bmp;*.gif;*.jpeg;*.jpg;*.png";
            openFileDialog.RestoreDirectory = true;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                FileInfo fileInfo = new FileInfo(openFileDialog.FileName);
                if (fileInfo.Length > 1024*GlobalVal.gloImageMaxSize)
                {
                    Method.MessageShow("W11020", dataName);//Image size must be less than 100 KB.
                    return;
                }
                else
                {
                    this.picUSER_PHOTO.Image = Image.FromFile(openFileDialog.FileName);
                }
            }
        }

        /// <summary>
        /// btnPicClear_Click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPicClear_Click(object sender, EventArgs e)
        {
            this.picUSER_PHOTO.Image = null;
        }        

        /// <summary>
        /// dateTimePickerUSER_BIRTHDAY_CloseUp
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dateTimePickerUSER_BIRTHDAY_CloseUp(object sender, EventArgs e)
        {
            txtUSER_BIRTHDAY.Text = Method.TimeFormat(dateTimePickerUSER_BIRTHDAY.Value.ToString("yyyyMMdd"));
        } 
        #endregion
        
    }
}